package classes;

import com.mysql.jdbc.Connection;
import Servlet.dataBaseController;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Brian
 */
public class Employee {
    private static Connection con=null;
    private static ResultSet rs=null;
    private static String query="SELECT * FROM brchung.employee E, brchung.person P WHERE E.SSN=P.SSN and P.role='e';";
    public Employee()
    {}
    public static void getConnection() throws SQLException
    {
        try{
            con=dataBaseController.getConnection();
        try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
            }
            Statement st = (Statement) con.createStatement();
            rs=st.executeQuery(query);
            
            //st.close();
            //con.close();
        }
        catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    public static ArrayList getResults(int... column)
    {
        ArrayList<String> al= new ArrayList<String>();
        try{
            while(rs.next())
            {
                String temp="";
                for(int i=0;i<column.length;i++)
                {
                    if(column.length<=1)
                        if(column[i]==13)
                        {
                            temp=rs.getString("(id: "+column[i]+")");
                        }
                        else
                            temp=rs.getString(column[i]);
                    else
                        if(column[i]==13)
                        {
                            temp+="(id: "+rs.getString(column[i])+") ";
                        }
                        else
                            temp+=rs.getString(column[i])+" ";
                }
                al.add(temp);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return al;
    }
    
    public static void removeEmployee(String SSN) throws SQLException
    {
         Statement st = con.createStatement();
        try{
            st.executeUpdate("DELETE FROM brchung.employee WHERE SSN='"+SSN+"';");
            st.executeUpdate("DELETE FROM brchung.person WHERE SSN='"+SSN+"';");
        } catch (Exception e) {
                        System.out.println(e.toString());
                }
    }
}
